﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IO;
using System.Net;
using WomanMotion.WebApi.Api.Utility;
using WomanMotion.WebApi.Infrastructure.DataBaseCommon;
using WomanMotion.WebApi.Infrastructure.UnifyResult;
using WomanMotion.WebApi.Model.Dtos;
using WomanMotion.WebApi.Model.Dtos.Classify;
using WomanMotion.WebApi.Model.Dtos.Team;
using WomanMotion.WebApi.Model.Dtos.Users;
using WomanMotion.WebApi.Model.Entitys;
using WomanMotion.WebApi.Service;
using WomanMotion.WebApi.Service.IService;

namespace WomanMotion.WebApi.Api.Controllers
{
    /// <summary>
    /// 分类管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [XSSFilter]
    [ApiController]
    [DescriptionName("分类管理", true, 50)]
    public class ClassifyController : BaseController
    {
        private readonly IClassifyService _classify;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="classifyService"></param>
        public ClassifyController(ILogger<ClassifyController> logger, IClassifyService classifyService) : base(logger)
        {
            _classify = classifyService;
        }

        /// <summary>
        /// 创建分类
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(SwaggerResultTemplate<ClassifyVM>), (int)HttpStatusCode.OK)]
        [DescriptionName("创建分类", true, "分类管理", "List", "ClassifyList")]
        public async Task<IResultOutput> Created(CreatedClassifyDTO dto)
        {
            var res = await _classify.CreatedAsync(dto);
            if (res != null)
            {
                return ResultOutput.Ok(res, "操作成功");
            }
            else
            {
                return ResultOutput.NotOk("操作失败");
            }
        }

        /// <summary>
        /// 启用分类
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(SwaggerResultTemplate), (int)HttpStatusCode.OK)]
        [DescriptionName("启用分类")]
        public async Task<IResultOutput> Enable(PrimaryKeyDTO ids)
        {
            var res = await _classify.UpdateDisableAsync(DisableEnum.Enable, ids.Ids);
            if (res > 0)
            {
                return ResultOutput.Ok(new object(), "操作成功");
            }
            else
            {
                return ResultOutput.NotOk("操作失败");
            }
        }

        /// <summary>
        /// 禁用分类
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(SwaggerResultTemplate), (int)HttpStatusCode.OK)]
        [DescriptionName("禁用分类")]
        public async Task<IResultOutput> Disable(PrimaryKeyDTO ids)
        {
            var res = await _classify.UpdateDisableAsync(DisableEnum.Disable, ids.Ids);
            if (res > 0)
            {
                return ResultOutput.Ok(new object(), "操作成功");
            }
            else
            {
                return ResultOutput.NotOk("操作失败");
            }
        }

        /// <summary>
        /// 更新分类
        /// </summary>
        /// <param name="dto">dto</param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(SwaggerResultTemplate<ClassifyVM>), (int)HttpStatusCode.OK)]
        [DescriptionName("更新分类")]
        public async Task<IResultOutput> Update(UpdateClassifyDTO dto)
        {
            var res = await _classify.UpdateAsync(dto);
            if (res == null)
            {
                return ResultOutput.NotOk("操作失败");
            }
            return ResultOutput.Ok(res, "操作成功");
        }

        /// <summary>
        /// 根据ID获取分类数据
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(SwaggerResultTemplate<ClassifyVM>), (int)HttpStatusCode.OK)]
        [DescriptionName("根据ID获取分类数据")]
        [AllowAnonymous]
        public async Task<IResultOutput> GetById(long id)
        {
            var res = await _classify.GetByIdAsync(id);
            if (res == null)
            {
                return ResultOutput.NotOk("没有数据");
            }
            return ResultOutput.Ok(res, "操作成功");
        }

        /// <summary>
        /// 根据ID获取多表连接分类信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(SwaggerResultTemplate<ClassifyVM>), (int)HttpStatusCode.OK)]
        [DescriptionName("根据ID获取多表连接分类信息")]
        [AllowAnonymous]
        public async Task<IResultOutput> GetCommonById(long id)
        {
            var res = await _classify.GetByIDNavAsync(id);
            if (res == null)
            {
                return ResultOutput.NotOk("没有数据");
            }
            return ResultOutput.Ok(res, "操作成功");
        }

        /// <summary>
        /// 分页获取分类数据
        /// </summary>
        /// <param name="pars">查询条件</param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(SwaggerResultTemplate<PageViewBase<List<ClassifyVM>>>), (int)HttpStatusCode.OK)]
        [DescriptionName("分页获取分类数据")]
        [AllowAnonymous]
        public async Task<IResultOutput> GetPageList(QueryParameterBase pars)
        {
            var res = await _classify.GetPageNavListAsync(pars);
            if (res.Data == null || res.Data.Count == 0)
            {
                return ResultOutput.NotOk("没有数据");
            }
            return ResultOutput.Ok(res, "操作成功");
        }

        /// <summary>
        /// 获取所有分类数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(SwaggerResultTemplate<List<ClassifyVM>>), (int)HttpStatusCode.OK)]
        [DescriptionName("获取所有分类数据")]
        [AllowAnonymous]
        public async Task<IResultOutput> GetList()
        {
            var res = await _classify.GetListNavAsync();
            if (res == null || res.Count == 0)
            {
                return ResultOutput.NotOk("没有数据");
            }
            return ResultOutput.Ok(res, "操作成功");
        }

        /// <summary>
        /// 获取没有禁用的所有一级分类数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(SwaggerResultTemplate<List<ClassifyVM>>), (int)HttpStatusCode.OK)]
        [DescriptionName("获取没有禁用的所有一级分类数据")]
        [AllowAnonymous]
        public async Task<IResultOutput> GetTopLevelList()
        {
            var res = await _classify.GetListNavAsync(m => m.Ifdisable == 1 && m.Parentid == 0);
            if (res == null || res.Count == 0)
            {
                return ResultOutput.NotOk("没有数据");
            }
            return ResultOutput.Ok(res, "操作成功");
        }

        /// <summary>
        /// 获取没有禁用的所有二级分类数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(SwaggerResultTemplate<List<ClassifyVM>>), (int)HttpStatusCode.OK)]
        [DescriptionName("获取没有禁用的所有二级分类数据")]
        [AllowAnonymous]
        public async Task<IResultOutput> GetTwoLevelList()
        {
            var res = await _classify.GetListNavAsync(m => m.Ifdisable == 1 && m.Parentid != 0);
            if (res == null || res.Count == 0)
            {
                return ResultOutput.NotOk("没有数据");
            }
            return ResultOutput.Ok(res, "操作成功");
        }

        /// <summary>
        /// 获取传入的一级分类所属的二级分类数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(SwaggerResultTemplate<List<ClassifyVM>>), (int)HttpStatusCode.OK)]
        [DescriptionName("获取传入的一级分类所属的二级分类数据")]
        [AllowAnonymous]
        public async Task<IResultOutput> GetSubordinatelList(long id)
        {
            var res = await _classify.GetListNavAsync(m => m.Ifdisable == 1 && m.Parentid == id);
            if (res == null || res.Count == 0)
            {
                return ResultOutput.NotOk("没有数据");
            }
            return ResultOutput.Ok(res, "操作成功");
        }

        /// <summary>
        /// 获取分类Tree数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(SwaggerResultTemplate<List<ClassifyTreeVM>>), (int)HttpStatusCode.OK)]
        [DescriptionName("获取分类Tree数据")]
        [AllowAnonymous]
        public async Task<IResultOutput> GetTreelList()
        {
            var res = await _classify.GetTreeListAsync();
            if (res == null || res.Count == 0)
            {
                return ResultOutput.NotOk("没有数据");
            }
            return ResultOutput.Ok(res, "操作成功");
        }
    }
}